package com.theaf.basemodule.service.impl;

import com.theaf.basemodule.common.api.DicService;
import com.theaf.basemodule.common.condition.DicCondition;
import com.theaf.basemodule.common.model.SysDic;
import com.theaf.basemodule.common.model.SysDicType;
import com.theaf.basemodule.common.vo.dic.DicDataVO;
import com.theaf.basemodule.common.vo.dic.DicVO;
import com.theaf.basemodule.dao.DicDAO;
import com.theaf.basemodule.dao.DicTypeDAO;
import com.theaf.core.cache.CachesHelper;
import com.theaf.core.repo.specification.SqlBuilder;
import com.theaf.core.repo.specification.SqlValue;
import com.theaf.core.utils.BeanMapper;
import com.theaf.core.utils.SmExceptionHelper;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DicServiceImpl implements DicService {
    public final static String CACHE_PREFIX="smyw_dictionry_";

    @Resource
    private DicTypeDAO dicTypeDAO;

    @Resource
    private DicDAO dicDAO;

    @Override
    public Page<DicVO> findByCondition(DicCondition condition) {
        Page<SysDicType> sysDics = dicTypeDAO.findCustom(condition);
        return sysDics.map(sysDicType->{
                DicVO dicVo = BeanMapper.map(sysDicType,DicVO.class);
                dicVo.setDatas(BeanMapper.mapList(sysDicType.getSysDicList(),DicDataVO.class));
                return dicVo;
            });
    }

    @Override
    public List<DicDataVO> findDataByTag(String tag) {
        SysDicType sysDic = dicTypeDAO.findOne(tag);
        if(sysDic==null){
            throw SmExceptionHelper.sysCheckException(tag+"字典找不到");
        }
        return sysDic.getSysDicList().parallelStream().map(
                n->BeanMapper.map(n,DicDataVO.class)).collect(Collectors.toList());
    }

    @Override
    public DicVO findByTag(String tag) {
        SysDicType sysDic = dicTypeDAO.findOne(tag);
        DicVO dicVO = BeanMapper.map(sysDic,DicVO.class);
        dicVO.setDatas(sysDic.getSysDicList().parallelStream().map(
                n->BeanMapper.map(n,DicDataVO.class)).collect(Collectors.toList()));
        return dicVO;
    }

    private int i;
    @Override
    public void save(DicVO dicVo) {
        //先删除无用数据
        dicDAO.deleteCustom(SqlBuilder.start("tag",SqlValue.parse(dicVo.getTag())));
        SysDicType sysDicType =  BeanMapper.map(dicVo,SysDicType.class);
        sysDicType.setSysDicList(new ArrayList<>());
        for(int i=0;i<dicVo.getDatas().size();i++){
            SysDic sysDic = BeanMapper.map(dicVo.getDatas().get(i),SysDic.class);
            sysDic.setTag(dicVo.getTag());
            sysDic.setOrderNo(i+1);
            System.out.println(sysDic.getCode());
            sysDicType.getSysDicList().add(sysDic);
        }
        //清除缓存
        CachesHelper.clearByPrefix(CACHE_PREFIX + dicVo.getTag());
        dicTypeDAO.save(sysDicType);
    }

    @Override
    public void delete( String tag) {
        SysDicType dicType = dicTypeDAO.findOne(tag);
        if(dicType==null){
            throw SmExceptionHelper.sysCheckException(tag+"字典找不到");
        }
        //清除缓存
        CachesHelper.clearByPrefix(CACHE_PREFIX + dicType.getTag());
        dicTypeDAO.delete(dicType);
    }
}
